package com.ming.sevice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.mapper.UserAddrMapper;
import com.ming.domain.UserAddr;
import com.ming.sevice.UserAddrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 *@Author:何元才
 *@Desc:
 *@data:2021-04-06 14:44
 */
@Service
@CacheConfig(cacheNames = "com.ming.service.impl.UserAddrServiceImpl")
public class UserAddrServiceImpl extends ServiceImpl<UserAddrMapper, UserAddr> implements UserAddrService{


    @Autowired
    private UserAddrMapper userAddrMapper;

    /**
     * 全查询用户收货地址
     * 可以使用缓存
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(key = "#userId")
    public List<UserAddr> getUserAddrList(String userId) {
        List<UserAddr> userAddrs = userAddrMapper.selectList(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getStatus, 1)
                .eq(UserAddr::getUserId, userId)
                .last(" ORDER BY common_addr desc , update_time desc ")
        );
        return userAddrs;
    }


    /**
     * 新增
     * 删除缓存
     *
     *  @param userAddr
     *  @return
     */
    @Override
    @CacheEvict(key = "#userAddr.userId")
    public boolean save(UserAddr userAddr) {
        Integer count = userAddrMapper.selectCount(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, userAddr.getUserId())
                .eq(UserAddr::getCommonAddr, 1)
        );
        if (count == 0) {
            // 如果之前没有 新增就是默认
            userAddr.setCommonAddr(1);
        }
        userAddr.setUpdateTime(new Date());
        userAddr.setCreateTime(new Date());
        userAddr.setStatus(1);
        userAddr.setVersion(0);
        return super.save(userAddr);
    }

    /**
     * 修改默认收货地址
     *
     * @param userId
     * @param addrId
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    @CacheEvict(key = "#userId")
    public void changeUserDefaultAddr(String userId, Long addrId) {
        // 1.查询之前的地址
        UserAddr oldAddr = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, userId)
                .eq(UserAddr::getCommonAddr, 1)
        );
        oldAddr.setCommonAddr(0);
        oldAddr.setUpdateTime(new Date());
        int update = userAddrMapper.updateById(oldAddr);
        if (update > 0){
            UserAddr newAddr = userAddrMapper.selectById(addrId);
            if (ObjectUtils.isEmpty(newAddr)){
                throw new IllegalArgumentException("修改的默认地址不存在");
            }
            newAddr.setCommonAddr(1);
            newAddr.setUpdateTime(new Date());
            userAddrMapper.updateById(newAddr);
        }
    }
}
